package com.light.mytext.sqlite

import android.content.ContentValues
import android.database.sqlite.SQLiteDatabase
import android.os.Bundle
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import com.light.mytext.R
import kotlinx.android.synthetic.main.activity_s_q_lite_test.*

/**
 * 数据库
 */
class SQLiteTestActivity : AppCompatActivity(), View.OnClickListener {
    private var db: SQLiteDatabase? = null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_s_q_lite_test)

        var openHelper = MyOpenHelper(this, "test.db1", null, 1)
        db = openHelper.writableDatabase//它会调用并返回一个可以读写数据库的对象
//        db!!.execSQL("DROP TABLE IF EXISTS "+ "user");//删除数据库
        init_Listener()
    }

    fun init_Listener() {
        bt_insert.setOnClickListener(this)
        bt_insertclear.setOnClickListener(this)
        bt_update.setOnClickListener(this)
        bt_updateclear.setOnClickListener(this)
        bt_delete.setOnClickListener(this)
        bt_deleteclear.setOnClickListener(this)
        bt_deleteall.setOnClickListener(this)
        bt_seeall.setOnClickListener(this)
    }

    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.bt_insert -> {//执行插入
                if (ed_insert.text.isEmpty()) {
                    return
                }
                var contentValues = ContentValues()
                contentValues.put("name", ed_insert.text.toString())
                db!!.insert("user", null, contentValues)
            }
            R.id.bt_insertclear -> {//清除
                ed_insert.text.clear()
            }
            R.id.bt_update -> {//更新
                if (ed_update_old.text.isEmpty() || ed_update_new.text.isEmpty()) {
                    return
                }
                var contentValues = ContentValues()
                contentValues.put("name", ed_update_new.text.toString())
                db!!.update("user", contentValues, "name=?", arrayOf(ed_update_old.text.toString()))
            }
            R.id.bt_updateclear -> {//清除
                ed_update_old.text.clear()
                ed_update_new.text.clear()
            }
            R.id.bt_delete -> {//执行删除
                if (ed_delete.text.isEmpty()) {
                    return
                }
                db!!.delete("user", "name=?", arrayOf(ed_delete.text.toString()))
            }
            R.id.bt_deleteclear -> {//清除
                ed_delete.text.clear()
            }
            R.id.bt_seeall -> {//查询所有
                //创建游标对象
                var cursor = db!!.query("user", arrayOf("name"), null, null, null, null, null)
                var tvData = StringBuffer("")
                while (cursor.moveToNext()) {
                    var name = cursor.getString(cursor.getColumnIndex("name"))
                    tvData.append("\n").append(name)
                }
                tv_content.text = tvData.toString()
            }
            R.id.bt_deleteall -> {//清除查询
                tv_content.text = ""
            }

        }

    }
}
